package UIFree {
	import UIFInterface.IComponent;
	
	import UIFree.FX.fx_runningProperty;
	
	import UIUtils.uif_util;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.filters.BitmapFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextLineMetrics;
	import flash.utils.Timer;
	import flash.utils.getQualifiedClassName;
	
	public class uif_component extends uif_draw implements IComponent {
		
		private var _width			: Number;
		private var _height			: Number;
		private var _styleCache		: Object;
		private var _hint			: Object;
		protected var _dragState	: Boolean;
		private var _light			: Number;
		private var _disable		: Boolean;
		
		protected var _styleClass		: String;
		protected var _runningProperty	: fx_runningProperty;
		
		public static const STARTDRAG		: String = 'STARTDRAG'; 
		public static const UPDATEHINT		: String = 'UPDATEHINT'; 
		public static const ENDDRAG			: String = 'ENDDRAG';
		public static const DRAG			: String = 'DRAG';
		public static const POSITION		: String = 'POSITION'; 
		public static const ROTATION		: String = 'ROTATION'; 
		public static const RESIZE			: String = 'RESIZE'; 
		public static const CHANGEVISIBLE	: String = 'CHANGEVISIBLE';
		public static const PAINT			: String = 'PAINT'; 
		public static const DISABLECHANGE	: String = 'DISABLECHANGE';
		
		public static const ALPHAFIELD			: String = 'alpha';
		public static const FIELD_STYLE			: String = 'style'; 
		public static const FIELD_FOCUSSTYLE	: String = 'focusStyle';
		public static const FIELD_SCALECENTER	: String = 'scaleCenter';
		
		private var __data: Object;
		
		public function uif_component(a_parent: uif_component, 
					ax: int=0, ay: int=0, 
					awidth: int=0, aheight: int=0, styleClass: Object='') {
			super();
			x 			= ax;
			y 			= ay;
			_light		= 1;
			_dragState 	= false;
			setStyle(styleClass);
			if (awidth && aheight) resize(awidth, aheight);
			if (a_parent) a_parent.addChild(this);
			_runningProperty = new fx_runningProperty(this);
		}
		
		public function get _data(): Object {
			return __data;
		}
		
		public function set _data(value: Object): void {
			setData(value);
		}
		
		protected function setData(value: Object): void {
			__data = value;
		}
		
		public function click(_handle: Function): void {
			addEventListener(MouseEvent.CLICK, function(e: MouseEvent): void {
				if (!disable) _handle(This);
			});
		}
		
		public function sysTrace(aMsg: Object): void {
			trace(this.toString() + ': ' + aMsg);		
		}
		
		public function get This(): * {
			return this;
		}
		
		public function get mousePos(): Point {
			return new Point(mouseX, mouseY);
		}
		
		public function get root_mousePos(): Point {
			return (!root)?(new Point(mouseX, mouseY)):(new Point(root.mouseX, root.mouseY));
		}
		
		public function asButton(): void {
			buttonMode = true;
			mouseChildren = false;
		}
		
		override public function set x(a_x:Number): void {
			setPosition(a_x, y);
		}
		
		override public function set y(a_y:Number): void {
			setPosition(x, a_y);
		}
		
		protected function doChangeVisible(a_set: Boolean): void {
			super.visible = a_set;
			dispatchEvent(new Event(CHANGEVISIBLE));
		}
		
		override public function set visible(a_set: Boolean): void {
			if (visible != a_set) doChangeVisible(a_set);
		}
		
		override public function get visible(): Boolean {
			return super.visible;
		}
		
		public function setVisibility(a_visible: Boolean): void {
			alpha 	= a_visible?1:0;
			visible = a_visible;
		}
		
		public function setPosition(a_x:Number, a_y:Number): void {
			doSetPosition(a_x, a_y);
		}
		
		public function setPositionInParent(a_x:Number, a_y:Number): void {
			if (parentComponent) {
				var inner: Rectangle = parentComponent.getInner();
				if (a_x < inner.x) a_x = inner.x;
				else if (a_x + width > inner.x + inner.width) a_x = inner.x + inner.width - width;
				if (a_y < inner.y) a_y = inner.y;
				else if (a_y + height > inner.y + inner.height) a_y = inner.y + inner.height - height;
				doSetPosition(a_x, a_y);
			}
		}
		
		protected function doSetPosition(a_x:Number, a_y:Number): void {
			super.x = a_x;
			super.y = a_y;
			dispatchEvent(new Event(POSITION));
		}
		
		public function setRect(rect: Rectangle): void {
			position = rect.topLeft;
			resize(rect.width, rect.height);
		}
		
		public function get right(): Number {
			return x + width;
		}
		
		public function get bottom(): Number {
			return y + height;
		}
		
		override public function set rotation(a_roll: Number): void {
			super.rotation = a_roll;
			dispatchEvent(new Event(ROTATION));
		}
		
		protected function callEventObject(eventObject: Object, params: Object=null, eventTarget: IEventDispatcher=null): void {
			if (eventObject is String) 
				(!eventTarget?eventTarget:This).dispatchEvent(new ObjectEvent(eventObject as String, params));
			else if (eventObject is Function) (eventObject as Function)(params);
		}
		
		protected function afterStage(func: Function): void {
			function doAfterStage(e: Event): void {
				removeEventListener(Event.ADDED_TO_STAGE, doAfterStage);
				func();
			}
			if (stage) func()
			else addEventListener(Event.ADDED_TO_STAGE, doAfterStage);
		}
		
/*		protected function fxCompute(a_from: *, a_to: *, a_delta: Number, asColor: Boolean = false): * {
			if ((a_from is Boolean) && (a_to is Boolean)) {
				return a_delta<0.5?a_from:a_to;
			} else if ((a_from is Number) && (a_to is Number)) {
				if (asColor) return uif_color.computeRGB(a_from, a_to, a_delta);
				else return (a_to - a_from) * a_delta + a_from;
			} else if ((a_from is String) && (a_to is String))
				return a_delta<0.5?a_from:a_to;
			else  if ((a_from is Array) && (a_to is Array))
				return fxComputeArray(a_from, a_to, a_delta, asColor);
			else  if ((a_from is Object) && (a_to is Object))
				return fxComputeObject(a_from, a_to, a_delta, asColor);
			else return null;
		}
		
		protected function fxComputeObject(a_from: Object, a_to: Object, a_delta: Number, asColor: Boolean = false): Object {
			var result: Object = union(a_from, {});
			for (var i: String in a_to) {
				if (a_from.hasOwnProperty(i)) {
					result[i] = fxCompute(a_from[i], a_to[i], a_delta, 
						(i == 'colors') || (i == 'color') || (i == 'line_color') || asColor);
				}
			}
			return result;
		}
		
		protected function fxComputeArray(a_from: Array, a_to: Array, a_delta: Number, asColor: Boolean = false): Array {
			var result: Array = a_from.concat();
			for (var i: uint=0; i<a_to.length; i++) {
				result[i] = fxCompute(a_from[i], a_to[i], a_delta, asColor);
			}
			return result;
		}*/
		
		public function FXCancel(): void {
			_runningProperty.FXCancel();
		}
		
		public function FXNameCancel(propertyName: String): void {
			_runningProperty.FXNameCancel(propertyName);
		}
		
		public function getFX(propertyName: String): FX {
			return _runningProperty.getFX(propertyName);
		}
		
		public function fx(propertyName: String, a_from: *, a_to:*, 
								a_transition: Function=null, fps: int =16, 
								duration: int=500): FX {
			return _runningProperty.fx(propertyName, a_from, a_to, a_transition, fps, duration);
		}
		
		public function styleFX(a_from: *, a_to:*, 
								a_transition: Function=null, fps: int =16, 
								duration: int=500): FX {
			return fx(FIELD_STYLE, app.styleByObj(a_from), app.styleByObj(a_to), a_transition, fps, duration);
		}
		
		public function alphaFX(alphaTo: Number, doEndFX: Function = null,
								a_transition: Function=null, fps: int =16, 
								duration: int=500): void {
			FXNameCancel(ALPHAFIELD);
			fx(ALPHAFIELD, alpha, alphaTo, a_transition, fps, duration).start(doEndFX);
		}
		
		public function get hitMouse(): Boolean {
			return stage?hitTestPoint(app.mouseX, app.mouseY):false;
		}
		
		public function blink(sec: Number = 5): void {
			var timer: Timer = new Timer(20, sec * 1000 / 20);
			var bindex: int = 0;
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, function(e: TimerEvent): void {
				light = 1;
			});
			timer.addEventListener(TimerEvent.TIMER, function(e: TimerEvent): void {
				bindex++;
				light = 1 + Math.sin(bindex * 0.5) / 2; 
			});
			timer.start();
		}
		
		public function get light(): Number {
			return _light;
		}
		
		public function set light(a_light: Number): void {
			_light = a_light;
			transform.colorTransform = new ColorTransform(_light, _light, _light, alpha);
		}
		
		protected function checkDisable(): Boolean {
			return _disable || (parent && (parent is uif_component) && parent['checkDisable']());
		}  
		
		public function get disable(): Boolean {
			return checkDisable();
		}
		
		protected function doDisable(a_set: Boolean): void {
			_disable = a_set;
			disableVisualization();
			dispatchEvent(new Event(DISABLECHANGE));
		}
		
		protected function disableVisualization(): void {
			if (!_disable) transform.colorTransform = new ColorTransform();
			else transform.colorTransform = new ColorTransform(0.5, 0.5, 0.5, 1, 127, 127, 127);
		}
		
		public function set disable(a_set: Boolean): void {
			if (_disable != a_set) doDisable(a_set);
		}
		
		public function get hint(): Object {
			return _hint;
		}
		
		public function set hint(a_hint: Object): void {
			_hint = a_hint;
			dispatchEvent(new Event(UPDATEHINT));
		}
		
		public function get style(): Object {
			return _styleCache?_styleCache:(_styleCache=app.styleByName(_styleClass));
		}
		
		public function set style(a_style: Object): void {
			applyStyle(a_style); 
		}
		
		protected function stParam(paramName: String, defValue: * = undefined) :* {
			if (style.hasOwnProperty(paramName))
				return style[paramName];
			else return defValue;
		}
		
		protected function applyChildStyle(childName : String): void {
			if (style.hasOwnProperty(childName)) {
				this[childName].setStyle(style[childName]);
			}
		}
		
		protected function applyChildsStyle(): void {
			for (var prop: String in style)
				if (this.hasOwnProperty(prop) && (this[prop] is uif_component))
					applyChildStyle(prop);
		}
		
		protected function applyStyle(a_style: Object): void {
			var i: String;
			_styleCache = uif_util.unionObj(style, app.styleByObj(a_style));
			
			if (style.component) uif_util.setValues(This, style.component);

			if (style.filters) {
				clearFilters();			
				for (i in style.filters) 
					this[i](style.filters[i]);
			}
			repaint();
			applyChildsStyle();
		}
		
		protected function setStyle(styleName: Object): void {
			var newStyle: Object;
			if (styleName) {
				if (styleName is String) {
					if (styleName && (_styleClass != styleName)) {
						if ((newStyle = app.styleByName(styleName as String)) != null) {
							_styleClass = styleName as String;
							_styleCache = null;
							applyStyle(newStyle);
						}
					}
				} else {
					_styleClass = '';
					applyStyle(styleName);	
				}
			}
		}
		
		public function get styleClass(): String {
			return _styleClass;
		}
		
		public function set styleClass(newName: String): void {
			setStyle(newName);
//			filters = new Array();
//			repaint();
		}
		
		public function get app(): uif_app {
			return uif_app.instance();
		}
		
		override public function get width(): Number {
			return _width;
		}

		override public function set width(newValue: Number): void {
			resize(newValue, _height);
		}

		override public function get height(): Number {
			return _height;
		}
		
		public function get scale(): Number {
			return scaleX;
		}
		
		public function set scale(a_scale: Number): void {
			scaleX = a_scale;
			scaleY = a_scale;
		}
		
		public function get center(): Point {
			return new Point(width / 2, height / 2);
		}
		
		public function toCenter(_relative: uif_component=null): void {
			_relative = _relative?_relative:(parent as uif_component);
			if (_relative) {
				var mat: Matrix = new Matrix();
				mat.translate((_relative.width - width) / 2, (_relative.height - height) / 2);
				transform.matrix = mat;
			}
		}
		
		public function set widthCenter(newValue: Number): void {
			x = x + (width - newValue) / 2;
			resize(newValue, _height);
		}
		
		public function set heightCenter(newValue: Number): void {
			y = y + (height - newValue) / 2;
			resize(_width, newValue);
		}
		
		public function set scaleCenter(a_scale: Number): void {
			x = x + (width * (scale - a_scale)) / 2; 
			y = y + (height * (scale - a_scale)) / 2;
			scale = a_scale; 
		}
		
		override public function set height(newValue: Number): void {
			resize(_width, newValue);
		}

		public function resize(newWidth: Number, newHeight: Number): uif_component {
			_width = newWidth;
			_height = newHeight;
			updateBounds();
			dispatchEvent(new Event(RESIZE));
			return this;
		}
		
		public function getOuter(): Rectangle {
			return new Rectangle(x, y, width, height);
		}
		
		public function getSpace(inner: Boolean = true): int {
			return app.getStyleSpace(style, inner);
		}
		
		public function getInner(incx: Number=0, incy: Number=0, incw:Number=0, inch:Number=0): Rectangle {
			var rect: Rectangle = new Rectangle(incx, incy, width + incw - incx, height + inch - incy);
			var _space: int = getSpace();
			rect.inflate(-_space, -_space);
			return rect;
		}
		
		public function getBorder(): Rectangle {
			var rect: Rectangle = new Rectangle(0, 0, width, height);
			var _space: int = getSpace(false);
			rect.inflate(-_space, -_space);
			return rect;
		}
		
		protected function paint(a_styleClass: String, a_method: String = ''): void {
			var rect: Rectangle = getBorder();
			var method: String = style.draw_method?
					(this.hasEventListener(style.draw_method)?style.draw_method:defaultDrawMethod()):
					(a_method?a_method:defaultDrawMethod());
			
			this[method](rect.x, rect.y, rect.width, rect.height, style);
			dispatchEvent(new Event(PAINT));
		}
		
		protected function defaultDrawMethod(): String {
			return uif_draw.DRAWRECTMETHOD;
		}
		
		protected function repaint(a_styleClass: String=''): void {
			a_styleClass = a_styleClass?a_styleClass:styleClass;
			if (_width && _height) {
				this.graphics.clear();
				paint(a_styleClass, defaultDrawMethod());
			}
		}
		
		protected function updateBounds(): uif_component {
			repaint(_styleClass);
			return this;
		}
		
		override public function getChildAt(index: int): DisplayObject {
			return (index<numChildren)?super.getChildAt(index):null;
		}
		
		public function forEach(proc: Function, a_class: Class = null): void {
			var i: int = 0;
			while (i < this.numChildren) { 
				var child: DisplayObject = super.getChildAt(i);
				var prevCount: int = this.numChildren;
				if ((a_class == null) || (child is a_class)) proc(child);
				i += 1 - (prevCount - this.numChildren);
			}
		}
		
		public function removeChilds(a_class: Class = null): void {
			forEach(function(item: DisplayObject): void {
				This.removeChild(item);
			}, a_class);
		}
		
		override public function set alpha(value:Number):void {
			if (value < 0.1) visible = false;
			else if (!visible) visible = true;
			
			super.alpha = value;
		}
		
		public function slideHide(stopHandle: Function = null): void {
//			if (visible)
			function doComplete(e: FXEvent=null): void {
				if (stopHandle != null) stopHandle();
			}
			if (alpha > 0)
				fx(ALPHAFIELD, alpha, 0).start(doComplete);
			else {
				FXNameCancel(ALPHAFIELD);
				doComplete();
			}
		}
	
		public function slideShow(stopHandle: Function = null, alphaTo: Number = 1): void {
//			var rfx: FX = getFX('alpha');
//			if (!visible// || rfx
//						) {
//				if (rfx) rfx.cancel();
//				visible = true;
//				alpha = 0;
			function doComplete(e: FXEvent=null): void {
				if ((alphaTo == 1) && (alpha >= 0.9)) {
					alpha = alphaTo;
				}
				if (stopHandle != null) stopHandle();
			}
			if (alpha != alphaTo)
				fx(ALPHAFIELD, alpha, alphaTo).start(doComplete);
			else {
				FXNameCancel(ALPHAFIELD);
				doComplete();
			}
//			}
		}
		
		public function slideVisible(a_visible: Boolean, endFunc: Function=null): void {
			if (a_visible) slideShow(endFunc);
			else slideHide(endFunc);
		}
		
		public function slideToggle(): void {
			if (visible) slideHide();
			else slideShow();
		}
		
		public function alignApply(align: *): void {
			if (typeof(align) == 'object') {
				for each (var aitem: String in align) alignApply(aitem);
			} else { 
				var pInner: Rectangle = (parent as uif_component).getInner();
				switch (align) {
					case 'left'		: x = pInner.x; 
										break;
					case 'right'	: x = pInner.x + pInner.width - width;
										break; 
					case 'top'		: y = pInner.y; 
										break; 
					case 'bottom'	: y = pInner.y + pInner.height - height; 
										break; 
				}
			}
		}
		
		public function addEmbed(a_class: Class, _x: int, _y:int): * {
			var embed:* = new a_class();
			embed.x = _x;
			embed.y = _y;
			addChild(embed);
			return embed;
		}
		
		public function EnterHere(size: Point): Rectangle {
			return uif_util.EnterHere(getInner(), size);
		}
		
		public function enterHereMatrix(size: Point): Matrix {
			var coordinates: Rectangle = EnterHere(size);
			var mat: Matrix = new Matrix();
			mat.scale(coordinates.width / size.x, coordinates.height / size.y);
			mat.translate(coordinates.x, coordinates.y);
			return mat;
		}
		
		public function beginDrag(game_beginner: int=1, dragScale: Number=1): void {
			var startp: Point = parentComponent.mousePos;
			var enddragFunc: Function = function(e: MouseEvent=null): void {
				appstage.removeEventListener(MouseEvent.MOUSE_MOVE, dragFunc);
				appstage.removeEventListener(MouseEvent.MOUSE_UP, enddragFunc);
				dispatchEvent(new Event(uif_component.ENDDRAG));
				_dragState = false;
			};
			
			var l_startDrag: Boolean = false;
			
			var dragFunc: Function = function(e: MouseEvent): void {
				var curp: Point = parentComponent.mousePos;
				if (!l_startDrag) {
					if (curp.subtract(startp).length >= game_beginner) {
						dispatchEvent(new Event(STARTDRAG));
						_dragState  = true;
						l_startDrag = true;
					}
				} else {
					if (_dragState) {
						var addPos: Point = curp.subtract(startp);
						if (updateDragPosition(x + addPos.x * dragScale, y + addPos.y * dragScale))
							startp = parentComponent.mousePos;
					} else enddragFunc();
				}
			}

			_dragState = false;
			l_startDrag = false;
			appstage.addEventListener(MouseEvent.MOUSE_MOVE, dragFunc);
			appstage.addEventListener(MouseEvent.MOUSE_UP, enddragFunc);
		}
		
		protected function updateDragPosition(n_x: Number, n_y: Number): Boolean {
			x = n_x;
			y = n_y;
			dispatchEvent(new Event(DRAG));
			return true;
		}
		
		public function get appstage(): EventDispatcher {
			return app.appstage;
		}
		
		public function endDrag(): void {
			_dragState = false;
		}
		
		public function get dragState(): Boolean {
			return _dragState;
		} 
		
		public function get coordinates(): Rectangle {
			return new Rectangle(x, y, width, height);
		}
		
		public function set coordinates(a_coord: Rectangle): void {
			x = a_coord.x;
			y = a_coord.y;
			width  = a_coord.width;
			height = a_coord.height;
		}
		
		public function get size(): Point {
			return new Point(width, height);
		}
		
		public function set size(a_size: Point): void  {
			resize(a_size.x, a_size.y);
		}
		
		
		public function get position(): Point {
			return new Point(x, y);
		}
		
		public function set position(a_p: Point): void {
			x = a_p.x;
			y = a_p.y;
		}
		
		public static function maxWidth(texts: Array, font: String, size: int): int {
			var max: int = 0;
			var field: TextField = createTextField(font, size);
			for each (var text: String in texts) {
				if (text) {
					field.text = text;
					if (max < field.textWidth) max = field.textWidth;	
				}
			} 
			return max;
		}
		
		public static function createTextField(font: String, size: int): TextField {
			var tf: TextFormat = new TextFormat(font, size);
			var field: TextField = new TextField();
			field.defaultTextFormat = tf;
			return field;
		}
		
		public static function getTextSize(text: String, font: String, size: int): Point {
			var field: TextField = createTextField(font, size);
			field.text = text;
			var tm: TextLineMetrics = field.getLineMetrics(0);
			return new Point(tm.width + 6, tm.height);
		}
		
		public function get parentComponent(): uif_component {
			return parent as uif_component;
		}
		
		public function applyFilter(filter: BitmapFilter): void {
			var l_filters: Array = filters;
			var isFilter: Boolean = false;
			var fIndex: int;
			if (l_filters.some(function(elem: BitmapFilter, index:int, arr: Array): Boolean {
					
					if (getQualifiedClassName(elem) == getQualifiedClassName(filter)) {
						fIndex = index;
						return true;
					} else return false
				})) {
				l_filters[fIndex] = filter;		
			} else l_filters.push(filter);
			filters = l_filters;
		}
		
		override public function removeFilter(filter:BitmapFilter):void {
			var l_filters: Array = filters;
			for (var i: int=0; i<l_filters.length; i++)
				if (getQualifiedClassName(l_filters[i]) == getQualifiedClassName(filter)) {
					l_filters.splice(i, 1);
					break;
				}
			filters = l_filters;
		}
		
/*		protected function checkMoveTo(): Boolean {
			return true;
		}
		
		public function moveTo(p: Point, transition: uif_transition=null, fps: uint=16, duration: uint=500): void {
			function doScrolling(): void {
				dispatchEvent(new Event(SCROLLING));
			}
			
			var cfx: FX;
			if ((cfx = getFX('x')) != null) cfx.cancel();
			if ((cfx = getFX('y')) != null) cfx.cancel();
			
			if (checkMoveTo(p.x, p.y)) {
				var st: Point = new Point(_area.x + p.x, _area.y + p.y);
				if (p.x != 0) fx('x', x, st.x, transition, fps, duration).start(doScrolling, function(e: FXEvent): void {
									x = st.x;
								});
				if (p.y != 0) fx('y', y, st.y, transition, fps, duration).start(doScrolling, function(e: FXEvent): void {
									y = st.y;
								});
			}
		}*/
		

		public function childToIndex(child: DisplayObject, a_index: int): void {
			swapChildren(child, getChildAt(a_index));
		}
		
		public function childToBack(child: DisplayObject): void {
			childToIndex(child, 0);
		}
		
		public function childToFront(child: DisplayObject): void {
			childToIndex(child, numChildren - 1);
		}
		
		public function get childBounds(): Rectangle {
			var rect: Rectangle = getInner();
			rect.width = 0;
			rect.height = 0;
			forEach(function(child: uif_component): void {
				rect = rect.union(child.getOuter());
			}, uif_component);
			return rect;
		}
		
		public function getEvent(get_event: String, set_event: String=''): Object {
			var result: *;
			function setFunc(e: ObjectEvent): void {
				result = e.object;
			}
			addEventListener(set_event, setFunc);
			dispatchEvent(new Event(get_event));
			removeEventListener(set_event, setFunc);
			return result;
		}
		
		public function waitEvent(event: String, eventProc: Function): void {
			function onEvent(e: Event): void {
				removeEventListener(event, onEvent);
				eventProc(e);
			}
			addEventListener(event, onEvent);
		}
		
/*		public function getBounds(toObject: Sprite=null): Rectangle {
			var result: Rectangle = new Rectangle();
			var my: Rectangle = new Rectangle(0, 0, width, height);
			result.topLeft 		= localToGlobal(my.topLeft);
			result.bottomRight 	= localToGlobal(my.bottomRight);
			if (toObject) {
				result.topLeft 		= toObject.globalToLocal(result.topLeft);
				result.bottomRight 	= toObject.globalToLocal(result.bottomRight);
			}
			
			return result;
		}*/

/*		public function get pinner(): Rectangle {
			return (parent as uif_component).getInner();
		}
		
		protected function ExCoord(aval): Object {
			var pp = aval.indexOf('+');
			var mp = aval.indexOf('-');
			var sp = mp;
			if (pp > mp) sp = pp;
			var _res = new Object();
			_res.inc = 0;
			if (sp > 0) {
				_res.inc  = Number(aval.substr(sp, aval.length));
				_res.value = aval.substr(0, sp);
			} else _res.value = aval;
			return _res;
		}
		
		protected function cnvCoordinate(aval1: *, c_name: String): Point {
			var toStr:String = new String(aval1);
			var s5 = toStr.substr(0, 5);
			if (s5 == 'float') {
				var aval = ExCoord(aval1);
				var float:String = aval['value'].substr(5, aval['value'].length);
				float = float.toLowerCase();
				var new_pos:Point = updateFloat(position, float);
				return new_pos[c_name] + aval['inc'];
			} else {
				if (typeof(aval1) == 'string') {
					var aval = ExCoord(aval1);
					if (aval['value'] == 'left')  		return pinner.x + aval['inc'];
					else if (aval['value'] == 'right')  return pinner.x + pinner.width - width + aval['inc'];
					else if (aval['value'] == 'center') return pinner.x + (pinner.width - width) / 2 + aval['inc'];
					else if (aval['value'] == 'top')    return pinner.y + aval['inc'];
					else if (aval['value'] == 'bottom') return pinner.y + pinner.height - height + aval['inc'];
					else if (aval['value'] == 'middle') return pinner.y + (pinner.height - height) / 2 + aval['inc'];
				} else return aval1;
			}
			return null;
		}*/		
	}
}